package com.sgy.handwrite_ioc_aop.factory;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2025/7/14 22:36
 * @description 使用反射技术生产对象
 */
public class BeanFactory {

    // 存储对象
    private static Map<String, Object> map = new HashMap<>();

    // 静态方法，类被加载的时候执行
    static {
        // 读取解析xml，通过反射技术实例化对象并存储待用
        InputStream resourceAsStream = BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml");
        // 读取器
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(resourceAsStream);
            Element rootElement = document.getRootElement();
            List<Element> beanList = rootElement.selectNodes("//bean");
            for (int i = 0; i < beanList.size(); i++) {
                Element element = beanList.get(i);
                // 获取每个bean元素的id和class
                String id = element.attributeValue("id");
                String aClass = element.attributeValue("class");
                // 通过反射技术实例化对象
                Class<?> aClass1 = Class.forName(aClass);
                // 实例化后的对象
                Object o = aClass1.newInstance();
                map.put(id, o);
            }

            // 实例化完成后完成对象的依赖关系
            // 有property子元素的bean就有传值需求
            List<Element> propertyList = rootElement.selectNodes("//property");
            for (int i = 0; i < propertyList.size(); i++) {
                Element element = propertyList.get(i);
                String name = element.attributeValue("name");
                String ref = element.attributeValue("ref");

                // 找到当前需要被处理依赖关系的bean
                Element parent = element.getParent();
                // 调用父元素对象的反射功能
                String id = parent.attributeValue("id");
                Object parentObject = map.get(id);
                // 遍历父对象中的所有方法
                Method[] methods = parentObject.getClass().getMethods();
                for (int j = 0; j < methods.length; j++) {
                    Method method = methods[j];
                    if (method.getName().equalsIgnoreCase("set" + name)) {
                        method.invoke(parentObject, map.get(ref));
                    }
                }

                // 把处理完成的parentObject重新放到map中
                map.put(id, parentObject);
            }
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    // 对外提供获取实例化对象的接口
    public static Object getBean(String id) {
        return map.get(id);
    }
}
